package com.lzy.netty.websocket;

import io.netty.channel.Channel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会话管理
 * 用户id和channel的关联处理
 * @author lzy
 */
public class UserChannelSession {

    // 用于多端同时接收信息，允许同一个账号在多个设备同时在线，比如ipad、iphone、电脑等设备同时接收信息
    // key:userId, value:多个端的channel
    private static Map<String, List<Channel>> multiSession = new HashMap<>();

    // 用于记录用户id和客户端channel长id的关系
    // key:channel长id, value:userId
    private static Map<String, String> userChannelIdRelation = new HashMap<>();

    public static void bindUser(String channelId, String userId) {
        // 绑定用户id和channel长id
        userChannelIdRelation.put(channelId, userId);
    }

    public static void unbindUser(String channelId) {
        // 删除用户id和channel长id的绑定关系
        userChannelIdRelation.remove(channelId);
    }
    public static String getUserIdByChannelId(String channelId) {
        return userChannelIdRelation.get(channelId);
    }

    public static void bindMultiSession(String userId, Channel channel) {

        List<Channel> channels = multiSession.get(userId);
        if(channels == null || channels.size() == 0){
            channels = new ArrayList<>();
        }
        channels.add(channel);

        multiSession.put(userId, channels);
    }

    public static void unbindMultiSession(String userId, String channelId) {

        List<Channel> channels = getMultiChannels(userId);
        if(channels == null || channels.size() == 0){
            return ;
        }

        // 删除
        for(int i = 0; i < channels.size(); i++){
            if(channels.get(i).id().asLongText().equals(channelId)){
                channels.remove(i);
                break;
            }
        }
        multiSession.put(userId, channels);
    }

    public static List<Channel> getMultiChannels(String userId){
        return multiSession.get(userId);
    }


    public static void outputMulti(){

        System.out.println("+++++++++++++++++++++++++++++++");
        for(Map.Entry<String, List<Channel> > entry: multiSession.entrySet()){

            System.out.println("------------------");
            System.out.println("用户id：" + entry.getKey());
            for(Channel channel: entry.getValue()){
                System.out.println("长id：" + channel.id().asLongText());
            }

            System.out.println("------------------");
        }

//        for(Map.Entry<String,String> entry: userChannelIdRelation.entrySet()){
//
//            System.out.println("------------------");
//            System.out.println("用户id：" + entry.getValue());
//            System.out.println("长id：" + entry.getKey());
//
//            System.out.println("------------------");
//        }
//        System.out.println("+++++++++++++++++++++++++++++++");

    }

    // 获取除了当前发送方的客户端channel，其余剩下的所有客户端channel
    public static List<Channel> getMyOtherChannels(String userId,String channelId){
       List<Channel> channels = getMultiChannels(userId);
       if(channels == null || channels.size() == 0){
           return null;
       }

       List<Channel> myOtherChannels = new ArrayList<>();
       for(int i = 0; i < channels.size(); i++){
           if(!channels.get(i).id().asLongText().equals(channelId)){
               myOtherChannels.add(channels.get(i));
           }
       }

       return myOtherChannels;
    }
}
